package com.atguigu.gmall.product.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 服务之间远程调用的控制层
 */

@RestController
@SuppressWarnings("all")
@RequestMapping("/api/product")
public class ProductApiController {
    @Autowired
    private SkuManageService skuManageService;
    @Autowired
    private SpuManageService spuManageService;
    @Autowired
    private BaseCategoryViewService baseCategoryViewService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 根据skuId查商品信息包含图片列表
     * @param skuId
     * @return
     */
    @GmallCache(prefix = "SkuInfo")
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuManageService.getSkuInfo(skuId);
        return skuInfo;
    }

    /**
     * 根据商品SKU三级分类id查询分类信息
     * @param category3Id
     * @return
     */
    @GmallCache(prefix = "BaseCategoryView")
    @GetMapping("/inner/getCategoryView/{category3Id}")
    public BaseCategoryView getCategoryView(@PathVariable("category3Id") Long category3Id){
       return baseCategoryViewService.getById(category3Id);
    }

    /**
     * 根据SKUID查询价格信息
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId){
       return skuManageService.getSkuPrice(skuId);
    }

    /**
     * 根据SPUID查询海报列表
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "SpuPosterBySpuId")
    @GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
    public List<SpuPoster> getSpuPosterBySpuId(@PathVariable("spuId") long spuId){
       return spuManageService.getSpuPosterBySpuId(spuId);
    }

    /**
     * 根据spuId获取当前平台属性和平台属性值
     * @param skuId
     * @return
     */
    @GmallCache(prefix = "AttrList")
    @GetMapping("/inner/getAttrList/{skuId}")
    public List<BaseAttrInfo> getAttrList(@PathVariable("skuId") Long skuId){
        List<BaseAttrInfo> list = skuInfoService.getAttrList(skuId);
        return list;
    }

    /**
     * 根据SPU查询所有销售属性,用销售属性来判断当前商品是否被选中
     * @param skuId
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "SpuSaleAttrListCheckBySku")
    @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(@PathVariable("skuId") Long skuId, @PathVariable("spuId") Long spuId){
       List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrListCheckBySku(skuId,spuId);
       return list;
    }

    /**
     * 查询每个商品SKU 销售属性和SKUID对照关系
     * @param spuId
     * @return
     */
    @GmallCache(prefix = "SkuValueIdsMap")
    @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
    public Map getSkuValueIdsMap(@PathVariable("spuId") Long spuId){
        Map<String,String> valueJsonMap = spuManageService.getSkuValueIdsMap(spuId);
        return valueJsonMap;
    }

    /**
     * 查询所有分类列表,分类嵌套结果:一级分类对象中包含二级分类集合,二级分类对象中包含二级分类集合
     * @return
     */
    @GetMapping("/inner/getBaseCategoryList")
    public List<JSONObject> getBaseCategoryList(){
        return baseCategoryViewService.getBaseCategoryList();
    }

    /**
     * 根据品牌id查询品牌信息
     * @param tmId
     * @return
     */
    @GetMapping("/inner/getTrademark/{tmId}")
    public BaseTrademark getTrademarkById(@PathVariable("tmId") Long tmId){
        BaseTrademark trademark =  baseTrademarkService.getById(tmId);
        return trademark;
    }
}